#!/usr/bin/expect -f

# ⬛️⬛️⬛️⬛️⬛️⬛️⬛️⬛️⬛️⬛️⬛️⬛️
# ⬛️环境准备：             ⬛️
# ⬛️brew install expect  ⬛️
# ⬛️安装 expect脚本工具    ⬛️
# ⬛️                     ⬛️
# ⬛️使用说明：            ⬛️
# ⬛️1.先build出dist文件夹 ⬛️
# ⬛️2.运行改脚本          ⬛️
# ⬛️⬛️⬛️⬛️⬛️⬛️⬛️⬛️⬛️⬛️⬛️⬛️

# 设置SSH连接参数
set user "root"
set host "124.71.74.229"
set port "22"
set password "Tyrone0."
set remote_dir "/usr/share/nginx/hephaestuses-question-bank-admin/"




#启动 scp 命令
spawn scp -o StrictHostKeyChecking=no dist.tar.gz $user@$host:$remote_dir
expect "password"
send "$password\r"
expect eof

#连接远程shell
spawn ssh -o StrictHostKeyChecking=no $user@$host
expect "password"
send "$password\r"
expect "$ "
send "mkdir -p $remote_dir && cd $remote_dir && tar -xzf dist.tar.gz && rm dist.tar.gz\r"
expect "$ "
send "sudo chown -R root:root dist\r"
expect "password for $user:"
send "$password\r"
expect "$ "
send "nginx -s reload\r"
expect "$ "
send "exit\r"
expect eof

puts "已部署至服务器"
